using System.Collections;
using System.Net;
using System.Text.RegularExpressions;
using System.Text;

namespace _8._1

{
    public partial class Form1 : Form
    {
        private string startUrl = "";
        private Crawler crawler;

        public Form1()
        {
            InitializeComponent();
            crawler = new Crawler();
        }

        private void crawlButton_Click(object sender, EventArgs e)
        {
            parsedListBox.Items.Clear();
            errorListBox.Items.Clear();

            startUrl = inputURLText.Text;
            crawler.urls.Add(startUrl, false);
            crawler.Crawl();

            foreach (var url in crawler.urlsList)
            {
                parsedListBox.Items.Add(url);
            }

            foreach (var error in crawler.errorList)
            {
                errorListBox.Items.Add(error);
            }
        }

        private void inputURLText_TextChanged(object sender, EventArgs e)
        {
            // You can add validation or other handling here if needed
        }
    }

    public class Crawler
    {
        public Hashtable urls = new Hashtable();
        private int count = 0;
        public List<string> urlsList = new List<string>();
        public List<string> errorList = new List<string>();

        public void Crawl()
        {
            while (true)
            {
                string current = null;
                foreach (string url in urls.Keys)
                {
                    if ((bool)urls[url] == true)
                    {
                        continue;
                    }
                    current = url;
                    break;
                }
                if (current == null || count > 10)
                    break;

                urlsList.Add(current);
                string html = DownLoad(current);
                urls[current] = true;
                count++;
                Parse(html, current);
            }
        }

        public string DownLoad(string url)
        {
            try
            {
                WebClient client = new WebClient();
                client.Encoding = Encoding.UTF8;
                string html = client.DownloadString(url);

                string fileName = count.ToString();
                File.WriteAllText(fileName, html, Encoding.UTF8);
                return html;
            }
            catch (Exception ex)
            {
                errorList.Add(ex.Message);
                return "";
            }
        }

        public void Parse(string html, string oldUrl)
        {
            string strRef = @"(href|HREF)[ ]*=[ ]*[""'](http|https)[^""'#>]+..html.*?[""']";

            MatchCollection matchCollection = new Regex(strRef).Matches(html);
            foreach (Match match in matchCollection)
            {
                var url = match.Value.Substring(match.Value.IndexOf('=') + 1).Trim('"', '\"', '#', ' ', '>');
                if (url.Length == 0)
                    continue;

                if (url.Contains("https://www.cnblogs.com"))
                {
                    if (urls[url] == null)
                        urls[url] = false;
                }
            }
        }
    }
}
